<?php
/**
 * Created by PhpStorm.
 * User: 苏徐
 * Date: 2017/9/10
 * Time: 15:36
 */

namespace App\Http\Controllers\User;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Routing\Controller;


class LoginController extends Controller
{
    function doLogin(){
        try{
            $input = Request::only('username', 'password');
            $validator = Validator::make($input, [
                'username' => 'required|max:16|min:2',
                'password' => 'required|max:16|min:4',
            ]);
            if ($validator->fails()) {
                return $validator->messages();
            }

            $where['username'] = $input['username'];
            $where['password'] = $input['password'];

            $result = DB::table('user')->where($where)->first();
            if($result){


                session_start();
                $_SESSION['isLogin'] =  $input['username'];
                return json_encode(['code'=>1,'msg'=>"验证成功"]);
            }else{
                return json_encode(['code'=>0,'msg'=>"验证失败，请重新登录"]);
            }

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }
    function doRegister(){
        var_dump("注册信息导入数据库");
    }
}